package com.glyxybxhtxt.service.impl;

import com.glyxybxhtxt.dao.DictMapper;
import com.glyxybxhtxt.dao.JdrMapper;
import com.glyxybxhtxt.dataObject.Jdr;
import com.glyxybxhtxt.service.JdrService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Author:wangzh
 * Date: 2020/12/3 21:11
 * Version: 1.0
 * describe：接单人 Service
 */
@Service
public class JdrServiceImpl implements JdrService {
    /**
     * 接单人 Mapper
     */
    @Resource
    private JdrMapper jdrMapper;

    @Resource
    private DictMapper dictMapper;

    /**
     * 查询接单人的数据
     */
    @Override
    public List<Jdr> selalljdr(List state) {
        return jdrMapper.selallJdr(state);
    }


    /**
     * 根据接单人易班id，删除该接单人的数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(String ybid) {
        jdrMapper.deleteByPrimaryKey(ybid);
    }

    /**
     * 插入一条接单人的数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void newjdr(Jdr j) {
        jdrMapper.insertSelective(j);
    }

    /**
     * 更新一条接单人的数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void upjdr(Jdr j) {
        jdrMapper.updateByPrimaryKeySelective(j);
    }

    @Override
    public List<Jdr> selOptimaljdr(String bxlb, Integer eid) {
        return jdrMapper.selOptimalJdr(bxlb,eid);
    }

    @Override
    public List<Jdr> selOptimaljdrPC(String bxlb, Integer eid) {
        List<Jdr> jdrs = jdrMapper.selOptimalJdrPC(bxlb, eid);
        // 如果没有合适的接单人，则执行以下的
        if (jdrs.size()==0){
            // 查询出所有签到的的接单人
            jdrs=jdrMapper.selNoQtjdr();
        }
        List<String> ybids = new ArrayList<>();
        List<Jdr> pcJdrs = jdrs.stream().filter(jdr -> {
            if (ybids.contains(jdr.getYbid())) {
                return false;
            } else {
                ybids.add(jdr.getYbid());
                return true;
            }
        }).collect(Collectors.toList());
        return pcJdrs;
    }
    @Override
    public List<Jdr> selNoQtjdr() {
        return jdrMapper.selNoQtjdr();
    }

    @Override
    public String selOpenidForYbid(String ybid) {
        // 用于查找用户绑定的ybid，如果没有绑定则返回null
        String openid=jdrMapper.selOpenidForYbid(ybid);
        if ("".equals(openid)){
            openid=null;
        }
        return openid;
    }
    @Override
    public int addOpenid(String id, String openid) {
        return jdrMapper.addOpenid(id,openid);
    }

    @Override
    public Jdr selUser(String id) {
        return jdrMapper.selUser(id);
    }

    @Override
    public List<Object> selOptimalBxlb() {

        // 查找出所有接单类型的id
        List<String> dictList=dictMapper.selAllDict();
        List<Object> dictList1=new ArrayList<>();
        //根据接单类型查找对应的接单人
        for (int i = 0; i < dictList.size(); i++) {
            // 报修类型id
            String dict=dictList.get(i);
            //保修类型名称
            String dictName=dictMapper.selDictNameForID(dict);
            //根据id查找合适的接单人
            List<Jdr> jdrList=jdrMapper.selOptimalBxlb(dict+"-");
            List<Object> jdrList1=new ArrayList<>();
            // 循环添加接单人到报修类别list
            for (int j = 0; j < jdrList.size(); j++) {
                Map<String,Object> jdrMap=new HashMap<>();
                jdrMap.put("value",jdrList.get(j).getYbid());
                jdrMap.put("label",jdrList.get(j).getXm());
                jdrList1.add(j,jdrMap);
            }
            Map<String,Object> dictMaps=new HashMap<>();
            dictMaps.put("value",dict);
            dictMaps.put("label",dictName);
            dictMaps.put("children",jdrList1);
            // 循环添加报修类别的信息到list
            dictList1.add(i,dictMaps);
        }

        return dictList1;
    }
}
